package solutions.leetcode.easy;

import solutions.lib.BaseSolution;
import solutions.lib.TreeNode;

/**
 * @author lizhidong
 * <a href="https://leetcode-cn.com/problems/binary-tree-tilt/">563. 二叉树的坡度</a>
 */
public class Solution563 extends BaseSolution {

    public int findTilt(TreeNode node) {
        count = 0;
        return Math.abs(sum(node.left) - sum(node.right)) + count;
    }

    int count = 0;

    private int sum(TreeNode node) {
        if (node == null) {
            return 0;
        }
        int left = sum(node.left);
        int right = sum(node.right);
        count += Math.abs(left - right);
        return left + right + node.val;
    }

    public static void main(String[] args) {
        Solution563 solution = new Solution563();
    }
}